<?php

namespace Xrfg\Api\Consume\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Xrfg\Core\Trading\Account\Models\Account as TradingAccount;
use Xrfg\Api\Consume\Models\ConsumeAccounts;
use Xrfg\Api\Consume\Models\ConsumeOrders;
use Xrfg\Api\Consume\Models\ConsumeRecommends;
use Xrfg\Api\Consume\Models\ConsumeProfits;
use Xrfg\Api\Consume\Requests\ConsumeRequest;
use Xrfg\Api\Consume\Job\ReturnXgJob;

class ConsumeController extends Controller
{
    public $XG_USDT = 0.1;
    // 购买消费券
    public function buyConsume(ConsumeRequest $request)
    {
        /** @var User $user */
        $user = Auth::user();
        $number = $request->input('number');
        // 消费券：100U+质押10个轩谷币
        $usdt_num = 100 * $number;
        $xg_num = 10 * $number;
        $count = 27;

        // 查询购买消费券数量超过count数量
        $order_count = ConsumeOrders::where('user_id',$user->id)->sum('number');
        if($order_count + $number > $count){
            $this->error(50001);
        }
        // usdt出账账户
        $usdtOutAccount = TradingAccount::firstOrCreate([
            'user_id' => $user->id,
            'coin_id' => 1,
        ])->refresh();
        if ($usdtOutAccount->available < $usdt_num) {
            $this->error(50002);
        }
        // xg出账账户
        $xgOutAccount = TradingAccount::firstOrCreate([
            'user_id' => $user->id,
            'coin_id' => 2,
        ])->refresh();
        if ($xgOutAccount->available < $xg_num) {
            $this->error(50003);
        }
        $parent_id = $user->parent_id;

        try {
            DB::transaction(function () use ($usdtOutAccount, $xgOutAccount, $usdt_num, $xg_num,$number,$parent_id) {
                $usdtOutAccount = TradingAccount::lockForUpdate()->find($usdtOutAccount->id);
                $xgOutAccount = TradingAccount::lockForUpdate()->find($xgOutAccount->id);
                // 1.生成用户订单
                $ConsumeOrders = new ConsumeOrders();
                $ConsumeOrders->user_id = Auth::id();
                $ConsumeOrders->number = $number;
                $ConsumeOrders->parent_id = $parent_id;
                $ConsumeOrders->save();
                // 2.扣USDT
                $usdtOutAccount->availableDecrement($usdt_num);
                $usdtOutAccount->proofId = [$ConsumeOrders->id, 3, 8];
                $usdtOutAccount->save();

                // 3.扣XG
                $xgOutAccount->availableDecrement($xg_num);
                $xgOutAccount->proofId = [$ConsumeOrders->id, 3, 8];
                $xgOutAccount->save();
                // 4.质押XG
                $xgConsumeAccount = ConsumeAccounts::firstOrCreate([
                    'user_id' => Auth::id(),
                    'coin_id' => 2,
                ])->refresh();
                $xgConsumeAccount->freezeIncrement($xg_num);
                $xgConsumeAccount->type = '质押XG';
                $xgConsumeAccount->action_id = $ConsumeOrders->id;
                $xgConsumeAccount->save();
                // 5.新增usdt消费金额
                $usdtConsumeAccount = ConsumeAccounts::firstOrCreate([
                    'user_id' => Auth::id(),
                    'coin_id' => 1,
                ])->refresh();
                $usdtConsumeAccount = ConsumeAccounts::lockForUpdate()->find($usdtConsumeAccount->id);
                // 6.新增收益订单明细
                $consumeProfit = new ConsumeProfits();
                $consumeProfit->user_id = Auth::id();
                $consumeProfit->order_id = $ConsumeOrders->id;
                $consumeProfit->coin_id = 1;
                $consumeProfit->rate = 0;
                $consumeProfit->profit = $number*150;
                $consumeProfit->number = $number*100;
                $consumeProfit->save();
                $usdtConsumeAccount->availableIncrement($number*100);
                // 7.新增usdt冻结收益金额
                $usdtConsumeAccount->disabledIncrement($number*150);
                $usdtConsumeAccount->type = '购买消费券';
                $usdtConsumeAccount->action_id = $ConsumeOrders->id;
                $usdtConsumeAccount->save();
                // 8.新增xg冻结收益金额
                $xgConsumeAccount = ConsumeAccounts::lockForUpdate()->find($xgConsumeAccount->id);
                $xgConsumeAccount->disabledIncrement($number*(150/$this->XG_USDT));
                $xgConsumeAccount->type = '购买消费券';
                $xgConsumeAccount->action_id = $ConsumeOrders->id;
                $xgConsumeAccount->save();
                // 7.上级收益
                $this->parentAddProfit($ConsumeOrders); // 上级收益
                // 8.层级收益
                (new ReturnXgJob($ConsumeOrders,$this->XG_USDT))->handle(); // 层级收益
            });
        } catch (\Exception $exception) {
            Log::error($exception);
            $this->error($exception->getCode());
        }

        return $this->null();
    }

    // 上级收益
    public function parentAddProfit($consumeOrder)
    {
        // 返XG明细
        $recommend = new ConsumeRecommends();
        $recommend->user_id = $consumeOrder->parent_id;
        $recommend->child_user_id = $consumeOrder->user_id;
        $recommend->order_id = $consumeOrder->id;
        $recommend->total = 0;
        $recommend->rate = 0;
        $recommend->type = 1;
        $recommend->profit = $consumeOrder->number * (50/$this->XG_USDT);
        $recommend->time = now();
        $xgConsumeAccount = ConsumeAccounts::firstOrCreate([
            'user_id' => $consumeOrder->parent_id,
            'coin_id' => 2,
        ])->refresh();
        DB::transaction(function () use ($recommend, $xgConsumeAccount) {
            $xgConsumeAccount = ConsumeAccounts::lockForUpdate()->find($xgConsumeAccount->id);
            $recommend->save();
            // 更新账户
            $xgConsumeAccount = ConsumeAccounts::lockForUpdate()->find($xgConsumeAccount->id);
            $xgConsumeAccount->disabledIncrement($recommend->profit);
            $xgConsumeAccount->type = '推人收益';
            $xgConsumeAccount->action_id = $recommend->order_id;
            $xgConsumeAccount->save();
        });

    }
}
